﻿package export.garden
{
	import com.dusk.util.RandomUtil;
	import com.dusk.util.TextUtil;
	import com.game.manager.NotificationManager;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public dynamic class HarvestPlant extends MovieClip
	{
		
		
		public function HarvestPlant(param1:uint, param2:String)
		{
			super();
			TextUtil.redrawTxtOld(this, ["contentTxt", "oddsTxt"]);
			this.gardenLv = param1;
			this.type = param2;
			this.curLevel = 1;
			switch (this.type)
			{
				case "highCase":
					this.curAward = this.curAward.concat(this.highCaseAward);
					break;
				case "midCase":
					this.curAward = this.curAward.concat(this.midCaseAward);
				case "lowCase":
					this.curAward = this.curAward.concat(this.lowCaseAward);
			}
			
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var closeBtn:SimpleButton;
		public var harvestBtn:SimpleButton;
		public var upgradeBtn:SimpleButton;
		public var contentTxt:TextField;
		public var oddsTxt:TextField;
		public var p1:MovieClip;
		public var p2:MovieClip;
		public var p3:MovieClip;
		public var p4:MovieClip;
		public var p5:MovieClip;
		private var type:String;
		private var lowCaseAward:Array = ["wpxt", "wptm", "wpsc"];
		private var midCaseAward:Array = ["wpll", "wpsg", "wplh", "wpxm", "wpdd"];
		private var highCaseAward:Array = ["flower_atk", "flower_crit", "flower_def", "flower_emp", "flower_hit", "flower_hp", "flower_miss"];
		private var curAward:Array = [];
		
		private var _gardenLv:uint;
		
		private function get gardenLv():uint
		{
			if (!isNaN(this._gardenLv - 53372))
			{
				return this._gardenLv - 53372;
			}
			else
			{
				return 0;
			}
		}
		
		private function set gardenLv(param1:uint):void
		{
			this._gardenLv = param1 + 53372;
		}
		
		private var _curLevel:uint;
		
		private function get curLevel():uint
		{
			return this._curLevel - 19833;
		}
		
		private function set curLevel(param1:uint):void
		{
			this._curLevel = param1 + 19833;
		}
		
		private function refreshDisplay():void
		{
			var i:int = 1;
			for (i; i <= 5; i++)
			{
				this["p" + i].gotoAndStop(this.type + "_de");
			}
			this["p" + this.curLevel].gotoAndStop(this.type + "_en");
			this.oddsTxt.text = "";
			this.oddsTxt.appendText("药园等级:" + this.gardenLv + "级\n");
			this.oddsTxt.appendText("升级成功概率:" + this.getUpOdds() + "%\n");
			this.oddsTxt.appendText("收获成功概率:" + this.getHarvestOdds() + "%");
			this.contentTxt.text = "可收获:";
			for each(var item:String in this.curAward)
			{
				this.contentTxt.appendText(Config.getIns().allEquip.findByName(item).name + ";");
			}
			
		}
		
		private function getUpOdds():int
		{
			return 100 - this.curLevel * 20;
		}
		
		private function getHarvestOdds():int
		{
			return this.gardenLv * 5 + this.curLevel * 9;
		}
		
		private function added(param1:Event):void
		{
			this.closeBtn.addEventListener(MouseEvent.CLICK, close);
			this.upgradeBtn.addEventListener(MouseEvent.CLICK, upgrade);
			this.harvestBtn.addEventListener(MouseEvent.CLICK, harvest);
			this.refreshDisplay();
		}
		
		private function removed(param1:Event):void
		{
			this.closeBtn.removeEventListener(MouseEvent.CLICK, close);
			this.upgradeBtn.removeEventListener(MouseEvent.CLICK, upgrade);
			this.harvestBtn.removeEventListener(MouseEvent.CLICK, harvest);
		}
		
		private function close(param1:MouseEvent):void
		{
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function upgrade(param1:MouseEvent):void
		{
			if (RandomUtil.randBoolean(this.getUpOdds()) && this.curLevel < 5)
			{
				this.curLevel++;
				NotificationManager.alert("升级成功");
			}
			else if (this.curLevel > 1)
			{
				this.curLevel--;
				NotificationManager.alert("升级失败");
			}
			this.refreshDisplay();
		}
		
		private function harvest(param1:MouseEvent):void
		{
			if (RandomUtil.randBoolean(this.getHarvestOdds()))
			{
				var _loc1_:String = this.curAward[Math.round(Math.random() * (this.curAward.length - 1))];
				var _loc2_:uint = Math.random() * 4 + 1;
				Config.getIns().putQhsInBackPack(Config.getIns().playerData, _loc1_, _loc2_);
				NotificationManager.alert("成功收获" + Config.getIns().allEquip.findByName(_loc1_).name + "x" + _loc2_);
			}
			else
			{
				NotificationManager.alert("收获失败");
			}
			this.close(null);
		}
	}
}
